<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <button @click="item='my-com1'">切换为组件1</button>
        <button @click="item='my-com2'">切换为组件2</button>
        <keep-alive include="component1">
            <component :is="item"></component>
        </keep-alive>
    </div>
    

    <template id="com1">
        <div>
            我是组件1
            <input type="text" >
        </div>
    </template>
    
    <template id="com2">
        <div>
            我是组件2
            <input type="text" >
        </div>
    </template>

    <script>
        const Com1 = {
            name: 'component1',
            template: '#com1',
            activated() {
                console.log('组件1被激活了')
            },
            deactivated() {
                console.log('组件1处于未激活状态')   
            }
        }

        const Com2 = {
            name: 'component2',
            template:'#com2'
        }

        new Vue({
            el: '#app',
            data: {
                item: 'my-com2'
            },
            components: {
                'myCom1': Com1,
                'myCom2': Com2
            }
        })
    </script>
</body>
</html>